package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SendCodeService;
import com.itheima.health.vo.SmsLoginParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mobile/login")
@Slf4j
public class LoginController {
    @DubboReference
    private SendCodeService sendCodeService;
    @DubboReference
    private MemberService memberService;

    @PostMapping("smsLogin")
    public Result smsLogin(@RequestBody SmsLoginParam smsLoginParam) {
        Boolean checkcode = sendCodeService.checkcode("MOBILE_SIGNIN", smsLoginParam.getTelephone(), smsLoginParam.getValidateCode());
        if (!checkcode) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    @PostMapping("checkLogin")
    public Result checkLogin(String telephone){
        Member member= memberService.findByTele(telephone);
        //2.判断用户是否为null
        if (null == member.getPhoneNumber() || !member.getPhoneNumber().equals(telephone)) {
            return new Result(false, "请登录");
        }
        //3.登录状态
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
}
